/***************************************************************************
 *  Copyright (c) 2005 Chih-Chung Chang                                    *
 *  Parts copyright (c) 2006 Michael Olson                                 *
 *  Copyright (C) 2007-2008 by Fehmi "Canavar" SAGLAM                      *
 *  canavar@fehmicans.net                                                  *
 *                                                                         *
 *  This file is part of sTerm.                                            *
 *                                                                         *
 *  sTerm is free software; you can redistribute it and/or modify          *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation; either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  sTerm is distributed in the hope that it will be useful,               *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>   *
 ***************************************************************************/

#include "../include/stermsection.h"

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
StermSection::StermSection(int rows, int cols, bool selectable)
{
	this->rows = rows;
	this->cols = cols;
	this->selectable = selectable;
	this->cur_button_i = -1;
	this->cur_button_j = -1;

	for(int i=0; i<3; ++i)
		for(int j=0; j<20; ++j)
			this->buttons[i][j] = NULL;
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
StermSection::~StermSection()
{
	for(int i=0; i<3; ++i)
		for(int j=0; j<20; ++j)
			if(this->buttons[i][j] != NULL)
			{
			   delete this->buttons[i][j];
			   this->buttons[i][j] = NULL;
         }
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
void StermSection::AddButton(StermButton *button,
		int row,
		int col)
{
	if(button == NULL)
		return;

	if(row < 0 || col < 0)
		return;

	button->Draw();
	this->buttons[row][col] = button;
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
void
StermSection::SelectButton( int button_i,
		int button_j
		)
{
	if(!this->isSelectable())
		return;

	if(button_i < 0 || button_j < 0 || button_i > 2 || button_j > 19)
		return;

	if(this->cur_button_i > 0 && this->cur_button_j > 0)  
		if(this->buttons[this->cur_button_i][this->cur_button_j] != NULL)
			buttons[this->cur_button_i][this->cur_button_j]->UnSelect();

	this->cur_button_i = button_i;
	this->cur_button_j = button_j;

	if(this->buttons[this->cur_button_i][this->cur_button_j] != NULL)
		this->buttons[this->cur_button_i][this->cur_button_j]->Select();
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
void StermSection::UnSelect()
{
	if(!this->isSelectable())
		return;

	if(this->cur_button_i < 0 || this->cur_button_j < 0)
		return;

	buttons[this->cur_button_i][this->cur_button_j]->UnSelect();
	this->cur_button_i = -1;
	this->cur_button_j = -1;
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
void StermSection::MoveUp()
{
	buttons[this->cur_button_i][this->cur_button_j]->UnSelect();

	if(this->cur_button_i == 0)
		this->cur_button_i = this->rows - 1;
	else
		this->cur_button_i--;   

	this->SelectButton(this->cur_button_i, this->cur_button_j);
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
void StermSection::MoveDown()
{
	buttons[this->cur_button_i][this->cur_button_j]->UnSelect();
	this->cur_button_i = (++this->cur_button_i)%this->rows; 
	this->SelectButton(this->cur_button_i, this->cur_button_j);
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
void StermSection::MoveLeft()
{
	buttons[this->cur_button_i][this->cur_button_j]->UnSelect();

	if(this->cur_button_j == 0)
		this->cur_button_j = this->cols - 1;
	else
		this->cur_button_j--; 

	this->SelectButton(this->cur_button_i, this->cur_button_j);
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
void StermSection::MoveRight()
{
	buttons[this->cur_button_i][this->cur_button_j]->UnSelect();
	this->cur_button_j = (++this->cur_button_j)%this->cols;
	this->SelectButton(this->cur_button_i, this->cur_button_j);
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
string StermSection::Get_Val()
{
	return this->buttons[this->cur_button_i][this->cur_button_j]->Get_Val();
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
string StermSection::Get_Shift_Val()
{
	return this->buttons[this->cur_button_i][this->cur_button_j]->Get_Shift_Val();
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
string StermSection::Get_Ctrl_Val()
{
	return this->buttons[this->cur_button_i][this->cur_button_j]->Get_Ctrl_Val();
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
bool StermSection::isSelectable()
{
	return this->selectable;
}
